<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

//Email Parameters
$recipient = $params->get('email_recipient', '');
$fromName = @$params->get('from_name', 'Rapid Contact');
$fromEmail = @$params->get('from_email', 'rapid_contact@yoursite.com');

// Text Parameters
$myNameLabel = $params->get('name_label', 'Name:');
$myEmailLabel = $params->get('email_label', 'Email:');
$mySubjectLabel = $params->get('subject_label', 'Subject:');
$myMessageLabel = $params->get('message_label', 'Message:');
$buttonText = $params->get('button_text', 'Send Message');
$pageText = $params->get('page_text', 'Thank you for your contact.');
$errorText = $params->get('error_text', 'Your message could not be sent. Please try again.');
$noEmail = $params->get('no_email', 'Please write your email');
$invalidEmail = $params->get('invalid_email', 'Please write a valid email');
$wrongantispamanswer = $params->get('wrong_antispam', 'Wrong anti-spam answer');
$pre_text = $params->get('pre_text', '');

// URL Parameters
$exact_url = $params->get('exact_url', true);
$disable_https = $params->get('disable_https', true);
$fixed_url = $params->get('fixed_url', true);
$myFixedURL = $params->get('fixed_url_address', '');

// Anti-spam Parameters
$enable_anti_spam = $params->get('enable_anti_spam', true);
$myAntiSpamQuestion = $params->get('anti_spam_q', 'How many eyes has a typical person?');
$myAntiSpamAnswer = $params->get('anti_spam_a', '2');

// Module Class Suffix Parameter
$mod_class_suffix = $params->get('moduleclass_sfx', '');


if ($fixed_url) {
  $url = $myFixedURL;
}
else {
  if (!$exact_url) {
    $url = JURI::current();
  }
  else {
    if (!$disable_https) {
      $url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
    }
    else {
      $url = "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
    }
  }
}

$myError = '';
$CORRECT_ANTISPAM_ANSWER = '';
$CORRECT_EMAIL = '';
$CORRECT_SUBJECT = '';
$CORRECT_MESSAGE = '';

if (isset($_POST["rp_email"])) {
  $CORRECT_SUBJECT = $_POST["rp_subject"];
  $CORRECT_MESSAGE = $_POST["rp_message"];
  // check anti-spam
  if ($enable_anti_spam) {
    if ($_POST["rp_anti_spam_answer"] != $myAntiSpamAnswer) {
      $myError = '<span  class="error_text">' . $wrongantispamanswer . '</span>';
    }
    else {
      $CORRECT_ANTISPAM_ANSWER = $_POST["rp_anti_spam_answer"];
    }
  }
  // check email
  if ($_POST["rp_email"] === "") {
    $myError = '<span class="error_text">' . $noEmail . '</span>';
  }
  if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", strtolower($_POST["rp_email"]))) {
    $myError = '<span class="error_text">' . $invalidEmail . '</span>';
  }
  else {
    $CORRECT_EMAIL = $_POST["rp_email"];
  }

  if ($myError == '') {
    $mySubject = $_POST["rp_subject"];
    $myMessage = 'You received a message from '. $_POST["rp_name"] .' ('. $_POST["rp_email"] .")\n\n". $_POST["rp_message"];

    $mailSender = &JFactory::getMailer();
    $mailSender->addRecipient($recipient);

    $mailSender->setSender(array($fromEmail,$fromName));
    $mailSender->addReplyTo(array( $_POST["rp_email"], '' ));

    $mailSender->setSubject($mySubject);
    $mailSender->setBody($myMessage);

    if ($mailSender->Send() !== true) {
      $myReplacement = '<span class="error_text">' . $errorText . '</span>';
      print $myReplacement;
      return true;
    }
    else {
      $myReplacement = '<span class="thanks_color">' . $pageText . '</span>';
      print $myReplacement;
      return true;
    }

  }
} // end if posted

// check recipient
if ($recipient === "") {
  $myReplacement = '<span class="error_text">No recipient specified</span>';
  print $myReplacement;
  return true;
}

print '<div class="rapid_contact ' . $mod_class_suffix . '"><form action="' . $url . '" method="post">';
if($pre_text != '') echo '<div class="intro_text ' . $mod_class_suffix . '">'.$pre_text.'</div>';

if ($myError != '') {
  print $myError;
}


print '<table width="100%" cellpadding="0" cellpadding="0">';
print '<tr><td width="50%"><input class=" inputbox ' . $mod_class_suffix . '" type="text" name="rp_name" value="' . $myNameLabel . '" onfocus="if (this.value==\'' . $myNameLabel . '\') this.value=\'\';" onblur="if (this.value==\'\') this.value=\'' . $myNameLabel . '\';" /></td>
<td width="50%" valign="top" rowspan="3"><textarea class=" textarea ' . $mod_class_suffix . '" name="rp_message">' . $myMessageLabel . '</textarea></td>
</tr>' . "\n";
print '<tr><td><input class=" inputbox ' . $mod_class_suffix . '" type="text" name="rp_email" value="' . $myEmailLabel . '" onfocus="if (this.value==\'' . $myEmailLabel . '\') this.value=\'\';" onblur="if (this.value==\'\') this.value=\'' . $myEmailLabel . '\';" /></td></tr>' . "\n";
print '<tr><td><input class=" inputbox ' . $mod_class_suffix . '" type="text" name="rp_subject" value="' . $mySubjectLabel .'" onfocus="if (this.value==\'' . $mySubjectLabel . '\') this.value=\'\';" onblur="if (this.value==\'\') this.value=\'' . $mySubjectLabel . '\';" /></td></tr>' . "\n";

if ($enable_anti_spam) {
    print '<tr><td><label for="rp_anti_spam_answer">' . $myAntiSpamQuestion . '</label>
	<input class=" captcha ' . $mod_class_suffix . '" type="text" name="rp_anti_spam_answer" id="rp_anti_spam_answer" value="'.$CORRECT_ANTISPAM_ANSWER.'"/></td>';
} else {
	echo '<tr>';
}

// print button
print '<td><input class=" button ' . $mod_class_suffix . '" type="submit" value="' . $buttonText . '" /></td></tr></table></form></div>' . "\n";
return true;
